package com.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.beans.*;
import com.dao.*;
import com.utils.PageInfo;
import com.utils.PageUtil;
import com.utils.StrUtil;


@WebServlet("/RoleServlet.do")
public class RoleServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	RoleDao _roleDao=new RoleDao();
	MenuDao _menuDao=new MenuDao();
	AdminDao _adminDao=new AdminDao();

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		
		String flag=request.getParameter("flag");
		if("manage".equals(flag)){
			manage(request,response);
		}else if("roleEdit".equals(flag)){
			roleEdit(request,response);
		}else if("updateRoleMenu".equals(flag)) {
			updateRoleMenu(request,response);
		}else if("addrole".equals(flag)){
			this.addRole(request,response);
		}else if("listadmin".equals(flag)){
			this.listAdmin(request,response);
		}else if("updateadminrole".equals(flag)){
			this.updateAdminRole(request,response);
		}else if("delete".equals(flag)){
			this.delete(request,response);
		}
	}
	//删除角色
		private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			int roleId=Integer.parseInt(request.getParameter("roleId"));
			
			//查询看看有没有用户在用这个角色,如果有,不能删除
			int adminCount = _roleDao.getAdminCountByRoleId(roleId);
			if(adminCount>0){
				request.setAttribute("msg", "还有 "+adminCount +" 个管理员在使用这个角色,角色信息删除失败 !");
			}
			else{
				request.setAttribute("msg", "角色信息删除成功!");
				_roleDao.deleteRole(roleId);
			}

			this.manage(request, response);
		}
	//更新用户角色
		private void updateAdminRole(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
			int roleId=Integer.parseInt(request.getParameter("roleId"));
			int adminId=Integer.parseInt(request.getParameter("adminId"));
			
			if(_roleDao.updateAdminRole(roleId,adminId)==1){
				request.setAttribute("msg", "用户角色信息更新成功!");
				request.getRequestDispatcher("perm/adminrole_edit.jsp").forward(request, response);
			}
			
		}
	//查询出所有用戶的信息,转到用户角色分配页
		private void listAdmin(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
			int pageIndex=1;
			
			String pageIndexStr=request.getParameter("pageIndex");
			if(!StrUtil.isNullOrEmpty(pageIndexStr)){
				pageIndex=Integer.parseInt(pageIndexStr);
			}
			
			int rowCount=_adminDao.getAdminCount();
			
			PageInfo page=PageUtil.getPageInfo(rowCount,10, pageIndex);
			List<AdminInfo> adminList=_adminDao.getAdminList(page);
			request.setAttribute("adminList", adminList);
			request.setAttribute("pageInfo", page);

			request.getRequestDispatcher("/perm/listadmin.jsp").forward(request, response);

		}
	//更新角色权限
	private void updateRoleMenu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int roleId=Integer.parseInt(request.getParameter("roleId"));
		String []  menuIds =request.getParameterValues("ids");
		
		_roleDao.updateRoleMenu(roleId,menuIds);
		request.setAttribute("msg", "修改成功");
		roleEdit(request,response);
		
		
	}
	//查询出菜单列表和角色信息,转到角色授权页面 	
	private void roleEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//查出角色信息
		int roleId=Integer.parseInt(request.getParameter("roleId"));
		RoleInfo roleInfo=_roleDao.getRoleInfoById(roleId);

		//查出菜单信息
		List<MenuInfo>menuList=_menuDao.getMenuList(0);
		//查询出角色有的权限，将权限数字转成字符串返回
		String menuIdStr=_roleDao.getMenuIdStr(roleId); 
		
		request.setAttribute("roleInfo", roleInfo);
		request.setAttribute("menuList", menuList);
		request.setAttribute("menuIdStr", menuIdStr);
		request.getRequestDispatcher("/perm/rolemenu_edit.jsp").forward(request, response);		
		
	}

	//查询角色列表
	private void manage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List<RoleInfo>  roleList= _roleDao.getRoleList();
		request.setAttribute("roleList", roleList);
		request.getRequestDispatcher("/perm/role_manage.jsp").forward(request, response);
		
	}
	//添加角色
	private void addRole(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		String roleName=request.getParameter("roleName");
		String des=request.getParameter("des");
		
		RoleInfo role=new RoleInfo();
		role.setRoleName(roleName);
		role.setDes(des);
		int result=_roleDao.addRole(role);
		if(result==1){
			request.setAttribute("msg", "角色添加成功!");
			request.getRequestDispatcher("/perm/role_add.jsp").forward(request,response);
		}
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doGet(request, response);
	}

}
